{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd    \n",
    "import numpy as np \n",
    "\n",
    "import pyecharts.options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "from pyecharts.charts import Line,Bar,Map,Pie,Grid,Tab,Timeline\n",
    "\n",
    "from pyecharts.globals import CurrentConfig, NotebookType\n",
    "CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB \n",
    "\n",
    "\n",
    "df_confirmed = pd.read_csv('data/china_confirmed.csv', header=0, index_col=0)\n",
    "df_death = pd.read_csv('data/china_death.csv', header=0, index_col=0)\n",
    "df_recovery = pd.read_csv('data/china_recovery.csv', header=0, index_col=0)\n",
    " \n",
    "# summary by date\n",
    "sum_confirmed = df_confirmed.sum(axis=1)\n",
    "sum_death = df_death.sum(axis=1)\n",
    "sum_recovery = df_recovery.sum(axis=1)\n",
    "# summary by distric\n",
    "sum_dist_confirmed = df_confirmed.sum(axis=0)\n",
    "sum_dist_death = df_death.sum(axis=0)\n",
    "sum_dist_recovery = df_recovery.sum(axis=0)\n",
    "\n",
    "\n",
    "accum_confirmed = sum_confirmed.cumsum()\n",
    "accum_death = sum_death.cumsum()\n",
    "accum_recovery = sum_recovery.cumsum()\n",
    "  \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " \n",
    "line_daily = Line(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "line_daily.add_xaxis(sum_confirmed.index.tolist())\n",
    "line_daily.add_yaxis('每日确诊',sum_confirmed.values.tolist(),label_opts=opts.LabelOpts(is_show=False))\n",
    "line_daily.add_yaxis('每日恢复',sum_recovery.values.tolist(),label_opts=opts.LabelOpts(is_show=False))\n",
    "line_daily.add_yaxis('每日死亡',sum_death.values.tolist(),label_opts=opts.LabelOpts(is_show=False))\n",
    " \n",
    "\n",
    "line_cum = Line(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "line_cum.add_xaxis(sum_confirmed.index.tolist())\n",
    "line_cum.add_yaxis('累计确诊',accum_confirmed.values.tolist(),label_opts=opts.LabelOpts(is_show=False))\n",
    "line_cum.add_yaxis('累计恢复',accum_recovery.values.tolist(),label_opts=opts.LabelOpts(is_show=False))\n",
    "line_cum.add_yaxis('累计死亡',accum_death.values.tolist(),label_opts=opts.LabelOpts(is_show=False))\n",
    "\n",
    "\n",
    "pie = Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "pie_data = [(sum_dist_confirmed.index[i],int(sum_dist_confirmed.values[i])) for i in range(len(sum_dist_confirmed))]\n",
    "pie.add('确诊病例分布', data_pair=pie_data)\n",
    "pie.set_global_opts(legend_opts=opts.LegendOpts(is_show=False)) \n",
    "\n",
    "pie_death = Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "pie_death_data = [(sum_dist_death.index[i],int(sum_dist_death.values[i])) for i in range(len(sum_dist_death))]\n",
    "pie_death.add('死亡病例分布', data_pair=pie_death_data)\n",
    "pie_death.set_global_opts(legend_opts=opts.LegendOpts(is_show=False))\n",
    "\n",
    ""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "outputPrepend"
    ]
   },
   "outputs": [],
   "source": [
    "visual_opts = opts.VisualMapOpts(max_=100000, is_piecewise=True,\n",
    "                                 pieces=[\n",
    "                                     {\"max\": 100000, \"min\": 10001,\n",
    "                                      \"label\": \">1000\", \"color\": \"#6A0C0C\"},\n",
    "                                     {\"max\": 10000, \"min\": 1001,\n",
    "                                      \"label\": \">1000\", \"color\": \"#8A0808\"},\n",
    "                                     {\"max\": 1000, \"min\": 500,\n",
    "                                      \"label\": \"500-1000\", \"color\": \"#FF0000\"},\n",
    "                                     {\"max\": 499, \"min\": 100,\n",
    "                                      \"label\": \"100-499\", \"color\": \"#B8860B\"},\n",
    "                                     {\"max\": 99, \"min\": 10,\n",
    "                                      \"label\": \"10-99\", \"color\": \"#EEB422\"},\n",
    "                                     {\"max\": 9, \"min\": 1, \"label\": \"1-9\",\n",
    "                                      \"color\": \"#EEDD82\"},\n",
    "                                     {\"max\": 0, \"min\": 0, \"label\": \"0\",\n",
    "                                      \"color\": \"#FFFFFF\"},\n",
    "                                 ])\n",
    "\n",
    "all_data = df_confirmed.cumsum()\n",
    "tl = Timeline()\n",
    "for day in all_data.index:  \n",
    "    s = (all_data.loc[day])\n",
    "    pairlst = [(s.index[i],int(s.values[i])) for i in range(len(s))]\n",
    "    map0 = (\n",
    "        Map(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "        .add(\"累计确诊\",pairlst, \"china\")\n",
    "        .set_global_opts(visualmap_opts=visual_opts) \n",
    "    )\n",
    "    tl.add(map0, day) \n",
    "    \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tab =  Tab()\n",
    "tab.add(tl,\"Covid-19地图\")\n",
    "tab.add(line_daily,\"每日新增\")\n",
    "tab.add(line_cum,\"累计病例\")\n",
    "tab.add(pie,\"各省确诊分布\")\n",
    "tab.add(pie_death,\"各省死亡病例分布\")\n",
    "\n",
    "tab.load_javascript()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tab.render_notebook() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}